#include "TimeInterval.h"

TimeInterval::TimeInterval()
: running(false)
{
}

void TimeInterval::start()
{
    start_time = std::chrono::high_resolution_clock::now();
    running    = true;
}

void TimeInterval::end()
{
    end_time = std::chrono::high_resolution_clock::now();
    running  = false;
}

long long TimeInterval::duration_ns()
{
    if(running) {
        // If timer is still running, return duration up to current time
        return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now() -
                                                                    start_time)
            .count();
    } else {
        // If timer has ended, return duration between start and end
        return std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - start_time).count();
    }
}

long long TimeInterval::duration_ms()
{
    return duration_ns() / 1000000;  // Convert nanoseconds to milliseconds
}
